package cn.boy.gui.adminstrator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import cn.boy.domain.Adminstrator;
import cn.boy.home.HomeApplication;
import cn.boy.home.R;
import cn.boy.service.HomeService;

public class AdminInfoEditActivity extends Activity {
	
	EditText txtNO;
	EditText txtName;
	EditText txtAge;
	EditText txtSex;
	EditText txtdepart;
	Button submitButton;
	
	HomeApplication app;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.admin_info_edit);
		
		findViews();
		
		initInfo();
		
		submitButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String no = txtNO.getText().toString();
				String name = txtName.getText().toString();
				String age = txtAge.getText().toString();
				String sex = txtSex.getText().toString();
				String department = txtdepart.getText().toString();
				Adminstrator admin = new Adminstrator(no, name, "", sex, age, department);
				app.setAdmin(admin);
				uploadInfo(admin);
				launchActivity();
				
			}

		
		});
	}
	
	private void uploadInfo(final Adminstrator admin) {
		new Thread(new Runnable() {
			
			public void run() {

				try {
					HomeService.updateInfo(admin);
				} catch (Exception e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
				
			}
		}).start();
		
	}
	
	private void launchActivity() {

		Intent intent = new Intent(AdminInfoEditActivity.this,AdminInfoActivity.class);
		startActivity(intent);
		finish();
	}

	private void initInfo() {

		app = (HomeApplication) getApplicationContext();
		Adminstrator admin = app.getAdmin();
		
		
		txtNO.setText(admin.getNO());
		txtName.setText(admin.getName());
		txtAge.setText(admin.getAge());
		txtSex.setText(admin.getSex());
		txtdepart.setText(admin.getDepartment());
		
	}

	private void findViews() {
		txtNO = (EditText)findViewById(R.id.et_admin_no);
		txtName = (EditText)findViewById(R.id.et_admin_name);
		txtAge = (EditText)findViewById(R.id.et_admin_age);
		txtSex = (EditText)findViewById(R.id.et_admin_sex);
		txtdepart = (EditText)findViewById(R.id.et_admin_depart);
		submitButton = (Button)findViewById(R.id.btn_submit);
	}
}
